package com.qinglei.recoup.longconn.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 物联网消息策略上下文
 *
 * @author yujunwei
 * @datetime 2022-03-02 17:18
 */
@Service
public class IotMessageStrategyContext {

    @Autowired
    private List<IotMessageStrategyService> iotMessageStrategyServiceList;

    private static Map<String, IotMessageStrategyService> iotMessageStrategyServiceMap;

    /**
     * 初始化策略接口
     */
    @PostConstruct
    public void init() {
        iotMessageStrategyServiceMap = iotMessageStrategyServiceList.stream().collect(
                Collectors.toMap(IotMessageStrategyService::protocolVersion, ims -> ims)
        );
    }

    /**
     * 获取策略接口
     */
    public IotMessageStrategyService getIotMessageStrategy(String protocolVersion) {
        return iotMessageStrategyServiceMap.get(protocolVersion);
    }
}
